﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Collections;
using Android.Graphics;
using Android.Content.Res;
using Android.Net;
using Android.Util;


namespace MWC.NetcampCheckin
{
    [Activity(Label = "Netcamp Ankieta")]
    public class NetcampFeedback : Activity
    {
        private bool IsUserFillUpFeedback()
        {
            RatingBar performance1 = FindViewById<RatingBar>(Resource.Id.performance1);
            RatingBar performance2 = FindViewById<RatingBar>(Resource.Id.performance2);
            RatingBar performance3 = FindViewById<RatingBar>(Resource.Id.performance3);
            EditText editQuestion1 = FindViewById<EditText>(Resource.Id.editQuestion1);
            EditText editQuestion2 = FindViewById<EditText>(Resource.Id.editQuestion2);
            return (performance1.Rating != 0 && performance2.Rating != 0 && performance3.Rating != 0 &&
                editQuestion1.Text != "" && editQuestion2.Text != "");

        }

        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.NetcampFeedback);

                List<TextView> listBold = new List<TextView>();
                listBold.Add(FindViewById<TextView>(Resource.Id.firstParenthasesOfTitle));
                listBold.Add(FindViewById<TextView>(Resource.Id.secondParanthasesOfTitle));
                listBold.Add(FindViewById<TextView>(Resource.Id.boldTextOfSubtitle));
                listBold.Add(FindViewById<TextView>(Resource.Id.boldTextOfTitle));
                setFont("fonts/texgyreheros-regular-webfont.ttf", listBold, TypefaceStyle.Bold);

                List<TextView> listNormal = new List<TextView>();
                listNormal.Add(FindViewById<TextView>(Resource.Id.normalTextOfSubtitle));
                listNormal.Add(FindViewById<TextView>(Resource.Id.normalTextOfTitle));
                listNormal.Add(FindViewById<TextView>(Resource.Id.ratePerformance));
                listNormal.Add(FindViewById<TextView>(Resource.Id.titlePerfomance1));
                listNormal.Add(FindViewById<TextView>(Resource.Id.titlePerfomance2));
                listNormal.Add(FindViewById<TextView>(Resource.Id.titlePerfomance3));
                listNormal.Add(FindViewById<TextView>(Resource.Id.titleEditQuestion1));
                listNormal.Add(FindViewById<TextView>(Resource.Id.titleEditQuestion2));
                listNormal.Add(FindViewById<TextView>(Resource.Id.titleEditQuestion3));

                setFont("fonts/texgyreheros-regular-webfont.ttf", listNormal, TypefaceStyle.Normal);

                RatingBar performance1 = FindViewById<RatingBar>(Resource.Id.performance1);
                RatingBar performance2 = FindViewById<RatingBar>(Resource.Id.performance2);
                RatingBar performance3 = FindViewById<RatingBar>(Resource.Id.performance3);
                EditText editQuestion1 = FindViewById<EditText>(Resource.Id.editQuestion1);
                EditText editQuestion2 = FindViewById<EditText>(Resource.Id.editQuestion2);
                EditText editQuestion3 = FindViewById<EditText>(Resource.Id.editQuestion3);

                Button sendvalue = FindViewById<Button>(Resource.Id.sendValues);
                sendvalue.Click += delegate
                {
                    if (IsUserFillUpFeedback())
                    {
                        //nie ma internetu
                        ConnectivityManager cm = (ConnectivityManager)GetSystemService(ConnectivityService);

                        if (cm.ActiveNetworkInfo == null || !cm.ActiveNetworkInfo.IsConnectedOrConnecting)
                        {
                            NetcampAlerts.NoInternet(this, false);
                        }
                        else
                        {
                            //wyslij wyniki
                            NetcampWebProxy.WebProxy proxy = new NetcampWebProxy.WebProxy();
                            proxy.Proxy = null;
                            List<float> ratings = new List<float>();
                            List<string> feedbacks = new List<string>();
                            ratings.Add(performance1.Rating);
                            ratings.Add(performance2.Rating);
                            ratings.Add(performance3.Rating);
                            feedbacks.Add(editQuestion1.Text);
                            feedbacks.Add(editQuestion2.Text);
                            feedbacks.Add(editQuestion3.Text);
                            proxy.SaveFeedbackAsync(ratings.ToArray(), feedbacks.ToArray());
                            NetcampAlerts.ThankForFeedback(this);

                            var proceed = new Intent(this, typeof(NetcampThankYou));
                            StartActivity(proceed);
                        }
                    }
                    else
                    {
                        Toast.MakeText(this, "Wypełnij wszystkie wymagane pola", ToastLength.Short).Show();
                    }
                };
            }
            catch (Exception e)
            {
                MWCApp.LogDebug(e);
                NetcampAlerts.UnknownError(this);
            }

        }

        private void setFont(string path, List<TextView> tTV, TypefaceStyle Type)
        {
           
            AssetManager mgr;
            mgr = Assets;
            Typeface font = Typeface.CreateFromAsset(mgr, path);
            for (int i = 0; i < tTV.Count; i++)
            {
                tTV[i].SetTypeface(font, Type);
            }

        }
    }
}